package com.wit.dao.impl;

import hibernate.HibernateSessionFactory;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.wit.dao.InsurancetypeDao;
import com.wit.po.Insurancetype;

public class InsurancetypeDaoImpl implements InsurancetypeDao {

	
	public boolean addInsurancetype(Insurancetype insurancetype) {
		Session s = null;
		Transaction tx = null;
		boolean isFinished = false;
		try{
			s = HibernateSessionFactory.getSession();
			tx =  s.beginTransaction();
			s.save(insurancetype);
			tx.commit();
			s.close();
			isFinished = true;
		}catch(HibernateException e){
			tx.rollback();
			e.printStackTrace();
		}finally{
			if(s.isOpen())
				s.close();
		}
		return isFinished;
	}

	
	public boolean deleteInsurancetype(Insurancetype insurancetype) {
		Session s = null;
		Transaction tx = null;
		boolean isFinished = false;
		try{
			s = HibernateSessionFactory.getSession();
			tx =  s.beginTransaction();
			s.delete(insurancetype);
			tx.commit();
			s.close();
			isFinished = true;
		}catch(HibernateException e){
			tx.rollback();
			e.printStackTrace();
		}finally{
			if(s.isOpen())
				s.close();
		}
		return isFinished;
	}

	
	public List<Insurancetype> findAllInsurancetype() {
		Session s = null;
		List<Insurancetype> compList = new ArrayList<Insurancetype>();
		try{
			s = HibernateSessionFactory.getSession();
			compList = (List<Insurancetype>)s.createQuery("FROM Insurancetype").list();
			s.close();
		}catch(HibernateException e){
			e.printStackTrace();
		}finally{
			if(s.isOpen())
				s.close();
		}
		return compList;
	}

	@SuppressWarnings("unchecked")
	
	public Insurancetype findInsurancetypeByIdenfier(String insuranceNumber) {
		Session s = null;
		Insurancetype insurancetype = null;
		try{
			s = HibernateSessionFactory.getSession();
			List<Insurancetype> compList = (List<Insurancetype>)s.createQuery("FROM Insurancetype WHERE InsuranceNumber = ?").setString(0, insuranceNumber).list();
			if(compList.size() == 1)
				insurancetype = compList.get(0);
			s.close();
		}catch(HibernateException e){
			e.printStackTrace();
		}finally{
			if(s.isOpen())
				s.close();
		}
		return insurancetype;
	}

	
	public boolean modifyInsurancetype(Insurancetype insurancetype) {
		Session s = null;
		Transaction tx = null;
		boolean isFinished = false;
		try{
			s = HibernateSessionFactory.getSession();
			tx =  s.beginTransaction();
			s.update(insurancetype);
			tx.commit();
			s.close();
			isFinished = true;
		}catch(HibernateException e){
			tx.rollback();
			e.printStackTrace();
		}finally{
			if(s.isOpen())
				s.close();
		}
		return isFinished;
	}

	
	public List<Insurancetype> findInsurancetypeBySuperiorid(String superior) {
		Session s = null;
		List<Insurancetype> compList = new ArrayList<Insurancetype>();
		try{
			s = HibernateSessionFactory.getSession();
			compList = (List<Insurancetype>)s.createQuery("FROM Insurancetype WHERE superiorid=?").setString(0, superior).list();
			s.close();
		}catch(HibernateException e){
			e.printStackTrace();
		}finally{
			if(s.isOpen())
				s.close();
		}
		return compList;
	}

}
